#ifndef PMANAGER_H
#define PMANAGER_H

#include <QObject>
#include <QtPlugin>
#include <QPluginLoader> //插件导入类
#include <QFile>
#include <QDebug>

#include "../LibOne/interface.h"

class PManager : public QObject
{
    Q_OBJECT
public:
    PManager(){}
    ~PManager()
    {
        if(m_pPluginIns!=nullptr) delete m_pPluginIns;
        if(m_pPluginDll!=nullptr) delete m_pPluginDll;
    }

    void LoadPlugin(QString FilePath)
    {
        if(QFile(FilePath).exists())
        {
            qDebug()<<"load dll path:"<<FilePath;
            QPluginLoader* m_pPluginDll = new QPluginLoader(FilePath);
            //m_pPluginDll->setFileName(FilePath)//设置dll
            //m_pPluginDll->load();//饿汉加载 return is bool
            QObject* Instance = m_pPluginDll->instance();//懒汉加载,官文写得很详细
            if(Instance!=nullptr)
            {
                qDebug()<<"load Plugin successful";
                qDebug()<<"metaData:"<<m_pPluginDll->metaData();//输出元数据信息
                m_pPluginIns = qobject_cast<Interface*>(Instance);
                connect(m_pPluginIns,SIGNAL(UsSigSend(QString)),this,SLOT(outputPluginMess(QString)));
                m_pPluginIns->dumpObjectInfo();//输出类的信息
                m_pPluginIns->Init();
            }
            else qDebug()<<"load Plugin error:"<<m_pPluginDll->errorString();//打印错误信息
        }
        else qDebug()<<"failed to select file path";
    }

signals:

public slots:
    void outputPluginMess(QString messStr)
    {
        qDebug()<<"im PManager,recv plugin mess is:" << messStr;
    }

private:
    QPluginLoader* m_pPluginDll= nullptr;
    Interface* m_pPluginIns = nullptr;
};

#endif // PMANAGER_H
